<script setup>
    import {ref, computed} from "vue"
    const shopList = ref([
      {id: 1, name: "篮球", num: 3},
      {id: 2, name: "足球", num: 8},
      {id: 3, name: "羽毛球", num: 2},
      {id: 4, name: "网球", num: 7},
      {id: 5, name: "棒球", num: 9},
    ])
    const totalNum = computed(() => {
      return shopList.value.reduce((prev, item) => item.num + prev, 0)
    })
    const transVal = computed({
      get() {
        return 1
      },
      set (val) {

        console.log("计算属性进行修改")
      }
    })
</script>

<template>
  <!-- 使用计算属性计算表格数据 -->
  <div>
    <table cellspacing="0">
      <thead>
        <tr>
          <th>ID</th>
          <th>名称</th>
          <th>数量</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in shopList" key="item.id">
          <td>{{ item.id }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.num }}</td>
        </tr>
      </tbody>
    </table>
  <p>购买商品的总数量是{{ totalNum }}</p>
  <button @click="transVal++">计算属性修改示例按钮</button>
  </div>
</template>

<style scoped>
  table {
    width: 350px;
  }
  table td, table th {
    border: 1px solid #ccc;
    text-align: center;
  }
</style>